
package com.magicalcoder.youyamvc.app.goodsimg.service.impl;

import com.magicalcoder.youyamvc.app.goodsimg.dao.GoodsImgDao;
import com.magicalcoder.youyamvc.app.goodsimg.service.GoodsImgService;
import com.magicalcoder.youyamvc.app.model.GoodsImg;
import com.magicalcoder.youyamvc.app.model.defined.GoodsImgWithGoods;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
*  欢迎加入官方QQ群:323237052
*/

@Component("goodsImgService")
public class GoodsImgServiceImpl implements GoodsImgService{
    @Resource(name="goodsImgDao")
    private GoodsImgDao goodsImgDao;


    @Override
    public GoodsImg getGoodsImg(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        return goodsImgDao.getGoodsImg(query);
    }

    @Override
    public GoodsImg selectOneGoodsImgWillThrowException(Map<String, Object> query){
        return goodsImgDao.getGoodsImg(query);
    }

    @Override
    public GoodsImg selectFirstGoodsImg(Map<String, Object> query){
        query.put("limit",1);
        List<GoodsImg> list = goodsImgDao.getGoodsImgList(query);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<GoodsImg> getGoodsImgList(Map<String, Object> query) {
        return goodsImgDao.getGoodsImgList(query);
    }

    @Override
    public Integer getGoodsImgListCount(Map<String, Object> query) {
        return goodsImgDao.getGoodsImgListCount(query);
    }

    @Override
    public     Long  insertGoodsImg(GoodsImg entity) {
        return goodsImgDao.insertGoodsImg(entity);
    }

    @Override
    public void updateGoodsImg(GoodsImg entity) {
        //校验
        goodsImgDao.updateGoodsImg(entity);
    }

    @Override
    public void updateGoodsImgWithoutNull(GoodsImg entity) {
        //校验
        goodsImgDao.updateGoodsImgWithoutNull(entity);
    }

    @Override
    public void updateGoodsImgByWhereSql(Map<String,Object> entity,String whereSql) {
        if(entity==null ||entity.isEmpty()){
            throw new RuntimeException("entity不能为空");
        }
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        entity.put("whereSql",whereSql);
        //校验
        goodsImgDao.updateGoodsImgByWhereSql(entity);
    }

    @Override
    public void deleteGoodsImg(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        goodsImgDao.deleteGoodsImg(query);
    }
    @Override
    public void deleteGoodsImgList(Map<String,Object> entity){
        goodsImgDao.deleteGoodsImgList(entity);
    }

    @Override
    public void deleteGoodsImgByWhereSql(String whereSql) {
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("whereSql", whereSql);
        goodsImgDao.deleteGoodsImgByWhereSql(query);
    }

    @Override
    public List<GoodsImgWithGoods> getGoodsImgWithGoodsList(Map<String, Object> query){
        return goodsImgDao.getGoodsImgWithGoodsList(query);
    }


    @Override
    public void truncateGoodsImg() {
        goodsImgDao.truncateGoodsImg();
    }

    @Override
    public void batchInsertGoodsImg(List<GoodsImg> list) {
        //校验
        goodsImgDao.batchInsertGoodsImg(list);
    }

    @Override
    public void batchUpdateGoodsImg(List<GoodsImg> list) {
        //校验
        goodsImgDao.batchUpdateGoodsImg(list);
    }
    @Override
    public void batchDeleteGoodsImg(List<Long> idList) {
        goodsImgDao.batchDeleteGoodsImg(idList);
    }

    @Override
    public void batchDeleteGoodsImgList(List<GoodsImg> entityList){
        goodsImgDao.batchDeleteGoodsImgList(entityList);
    }

    @Transactional
    @Override
    public void transactionImportJsonList(List<GoodsImg> list) {
        if(list!=null && list.size()>0){
            for(GoodsImg goodsImg : list){
                if (goodsImg.getId() == null) {
                    insertGoodsImg(goodsImg);
                } else {
                    GoodsImg entity = getGoodsImg(goodsImg.getId());
                    if(entity==null){
                        insertGoodsImg(goodsImg);
                    }else {
                        Copyer.copy(goodsImg, entity);
                        updateGoodsImg(entity);
                    }
                }
            }
        }
    }


    public List<GoodsImg> tryQueryList(String keyword,String selectValue, String foreignJavaField){
        List<GoodsImg> list;
        if(StringUtils.isBlank(keyword) || "全部".equals(keyword)){
            return getGoodsImgList(ProjectUtil.buildMap("limitIndex",0,"limit", 20));
        }else{
            list = searchList("imgNameFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            if(ProjectUtil.isNum(keyword)){
            list = searchList("goodsIdFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            }
            if(ProjectUtil.isNum(keyword)){
            list = searchList("typeFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            }
            list = searchList("statusFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return new ArrayList<GoodsImg>();
    }

    private List<GoodsImg> searchList(String field,String keyword){
        List<GoodsImg> list = getGoodsImgList(ProjectUtil.buildMap(field,keyword,"limitIndex",0,"limit", 20));
        if(ListUtils.isNotBlank(list)){
            return list;
        }
        String[] keys = keyword.split("-");
        for(String key:keys){
            list = getGoodsImgList(ProjectUtil.buildMap(field,key,"limitIndex",0,"limit", 20));
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return null;
    }
}
